//---------------------------------------------------------------------------

#include <vcl.h>
#include <windows.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CPort"
#pragma link "CPortCtl"
#pragma resource "*.dfm"

#define MOUSEEVENTF_MOVE 0x0001
#define MOUSEEVENTF_LEFTDOWN 0x0002
#define MOUSEEVENTF_LEFTUP 0x0004
#define MOUSEEVENTF_RIGHTDOWN 0x0008
#define MOUSEEVENTF_RIGHTUP 0x0010
#define MOUSEEVENTF_MIDDLEDOWN 0x0020
#define MOUSEEVENTF_MIDDLEUP 0x0040
#define MOUSEEVENTF_XDOWN 0x0080
#define MOUSEEVENTF_XUP 0x0100
#define MOUSEEVENTF_WHEEL 0x0800
#define MOUSEEVENTF_VIRTUALDESK 0x4000
#define MOUSEEVENTF_ABSOLUTE 0x8000

void ResetCounters();

int ScreenHeight, ScreenWidth,ComboBoxScreenSizeIndex,ComboBoxPortIndex;
AnsiString Data;
int CounterM, CounterE , CounterF, CounterG, CounterO, CounterC, CounterB, CounterA, CounterN, CounterH;
POINT Coordinates;
int a,b;
int WaitPlease;
int Accelerator;

TFormRemoteControl *FormRemoteControl;
//---------------------------------------------------------------------------
__fastcall TFormRemoteControl::TFormRemoteControl(TComponent* Owner)
        : TForm(Owner)
{

}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------

void __fastcall TFormRemoteControl::ButtonSettingsClick(TObject *Sender)
{
 ComPort->ShowSetupDialog();
}
//---------------------------------------------------------------------------
void __fastcall TFormRemoteControl::ButtonOpenPortClick(TObject *Sender)
{
 if (ComPort->Connected)
   {
    ComPort->Close();
    ButtonOpenPort->Caption="Open";
   }
    else
        {
         ComPort->Open();
         ButtonOpenPort->Caption="Close";
        }
}
//---------------------------------------------------------------------------

void MoveLeftUp()
    {
     GetCursorPos(&Coordinates);
     a=Coordinates.x;
     b=Coordinates.y;
     a=a-10;
     b=b-10;
     SetCursorPos(a,b);
    }

void MoveLeftUpFast()
    {
     GetCursorPos(&Coordinates);
     a=Coordinates.x;
     b=Coordinates.y;
     do
       {
        if ((a>3)&&(b>3))
          {
           a--;
           b--;
           SetCursorPos(a,b);
           Sleep(WaitPlease);
          }
       FormRemoteControl->ComPort->ReadStr(Data,1);
      }while ( (Data!='M') && (Data!='E') && (Data!='F') && (Data!='G') &&  (Data!='O') && (Data!='C') && (Data!='B') && (Data!='A') && (Data!='N') && (Data!='@') && (Data!='H') );
    CounterA=0;
    }

void MoveUp()
    {
     GetCursorPos(&Coordinates);
     a=Coordinates.x;
     b=Coordinates.y;
     b=b-10;
     SetCursorPos(a,b);
    }

void MoveUpFast()
    {
     GetCursorPos(&Coordinates);
     a=Coordinates.x;
     b=Coordinates.y;
     do
       {
        if (b>3)
          {
           b=b--;
           SetCursorPos(a,b);
           Sleep(WaitPlease);
          }
        FormRemoteControl->ComPort->ReadStr(Data,1);
       }while ( (Data!='M') && (Data!='E') && (Data!='F') && (Data!='G') &&  (Data!='O') && (Data!='C') && (Data!='B') && (Data!='A') && (Data!='N') && (Data!='@') && (Data!='H') );
    CounterM=0;
    }

void MoveRightUp()
    {
     GetCursorPos(&Coordinates);
     a=Coordinates.x;
     b=Coordinates.y;
     a=a+10;
     b=b-10;
     SetCursorPos(a,b);
    }

void MoveRightUpFast()
    {
     GetCursorPos(&Coordinates);
     a=Coordinates.x;
     b=Coordinates.y;
     do
       {
        if ((a<ScreenWidth-3)&&(b>3))
          {
           a=a++;
           b=b--;
           SetCursorPos(a,b);
           Sleep(WaitPlease);
          }
        FormRemoteControl->ComPort->ReadStr(Data,1);
       }while ( (Data!='M') && (Data!='E') && (Data!='F') && (Data!='G') &&  (Data!='O') && (Data!='C') && (Data!='B') && (Data!='A') && (Data!='N') && (Data!='@') && (Data!='H') );
    CounterE=0;
    }

void MoveRight()
    {
     GetCursorPos(&Coordinates);
     a=Coordinates.x;
     b=Coordinates.y;
     a=a+10;
     SetCursorPos(a,b);
    }

void MoveRightFast()
    {
     GetCursorPos(&Coordinates);
     a=Coordinates.x;
     b=Coordinates.y;
     do
       {
        if (a<ScreenWidth-3)
          {
           a=a++;
           SetCursorPos(a,b);
           Sleep(WaitPlease);
          }
        FormRemoteControl->ComPort->ReadStr(Data,1);
       }while ( (Data!='M') && (Data!='E') && (Data!='F') && (Data!='G') &&  (Data!='O') && (Data!='C') && (Data!='B') && (Data!='A') && (Data!='N') && (Data!='@') && (Data!='H') );
    CounterF=0;
    }

void MoveRightDown()
    {
     GetCursorPos(&Coordinates);
     a=Coordinates.x;
     b=Coordinates.y;
     a=a+10;
     b=b+10;
     SetCursorPos(a,b);
    }

void MoveRightDownFast()
    {
     GetCursorPos(&Coordinates);
     a=Coordinates.x;
     b=Coordinates.y;
     do
       {
        if ((a<ScreenWidth-3)&&(b<ScreenHeight-3))
          {
           a=a++;
           b=b++;
           SetCursorPos(a,b);
           Sleep(WaitPlease);
          }
       FormRemoteControl->ComPort->ReadStr(Data,1);
       }while ( (Data!='M') && (Data!='E') && (Data!='F') && (Data!='G') &&  (Data!='O') && (Data!='C') && (Data!='B') && (Data!='A') && (Data!='N') && (Data!='@') && (Data!='H') );
     CounterG=0;
     }

void MoveDown()
    {
     GetCursorPos(&Coordinates);
     a=Coordinates.x;
     b=Coordinates.y;
     b=b+10;
     SetCursorPos(a,b);
    }

void MoveDownFast()
    {
     GetCursorPos(&Coordinates);
     a=Coordinates.x;
     b=Coordinates.y;
     do
       {
        if (b<ScreenHeight-3)
          {
           b=b++;
           SetCursorPos(a,b);
           Sleep(WaitPlease);
          }
        FormRemoteControl->ComPort->ReadStr(Data,1);
      }while ( (Data!='M') && (Data!='E') && (Data!='F') && (Data!='G') &&  (Data!='O') && (Data!='C') && (Data!='B') && (Data!='A') && (Data!='N') && (Data!='@') && (Data!='H') );
    CounterO=0;
    }


void MoveLeftDown()
    {
     GetCursorPos(&Coordinates);
     a=Coordinates.x;
     b=Coordinates.y;
     a=a-10;
     b=b+10;
     SetCursorPos(a,b);
    }

void MoveLeftDownFast()
    {
     GetCursorPos(&Coordinates);
     a=Coordinates.x;
     b=Coordinates.y;
     do
       {
        if ((a>3)&&(b<ScreenHeight-3))
          {
           a=a--;
           b=b++;
           SetCursorPos(a,b);
           Sleep(WaitPlease);
          }
        FormRemoteControl->ComPort->ReadStr(Data,1);
       }while ( (Data!='M') && (Data!='E') && (Data!='F') && (Data!='G') &&  (Data!='O') && (Data!='C') && (Data!='B') && (Data!='A') && (Data!='N') && (Data!='@') && (Data!='H') );
    CounterC=0;
    }

void MoveLeft()
    {
     GetCursorPos(&Coordinates);
     a=Coordinates.x;
     b=Coordinates.y;
     a=a-10;
     SetCursorPos(a,b);
    }

void MoveLeftFast()
    {
     GetCursorPos(&Coordinates);
     a=Coordinates.x;
     b=Coordinates.y;
     do
       {
        if (a>3)
          {
           a=a--;
           SetCursorPos(a,b);
           Sleep(WaitPlease);
          }
        FormRemoteControl->ComPort->ReadStr(Data,1);
       }while ( (Data!='M') && (Data!='E') && (Data!='F') && (Data!='G') &&  (Data!='O') && (Data!='C') && (Data!='B') && (Data!='A') && (Data!='N') && (Data!='@') && (Data!='H') );
    CounterB=0;
    }

void HoldUp()
 {
  GetCursorPos(&Coordinates);
  a=Coordinates.x;
  b=Coordinates.y;
  mouse_event(MOUSEEVENTF_LEFTDOWN ,a,b,0,0);
 }

void HoldDown()
 {
  GetCursorPos(&Coordinates);
  a=Coordinates.x;
  b=Coordinates.y;
  mouse_event(MOUSEEVENTF_LEFTUP ,a,b,0,0);
 }

void ResetCounters()
    {
     CounterM=0;
     CounterE=0;
     CounterF=0;
     CounterG=0;
     CounterO=0;
     CounterC=0;
     CounterB=0;
     CounterA=0;
     CounterN=0;
     CounterH=0;
    }


void __fastcall TFormRemoteControl::ComboBoxScreenSizeSelect(TObject *Sender)
{
 ComboBoxScreenSizeIndex=ComboBoxScreenSize->ItemIndex;
 switch (ComboBoxScreenSizeIndex)
       {
        case 0:
               {
                ScreenWidth=800;
                ScreenHeight=600;
                FormRemoteControl->ShapeResolution->Width=80;
                FormRemoteControl->ShapeResolution->Height=60;
                FormRemoteControl->ShapeResolution->Brush->Color=clBlue;
                break;
               }
        case 1:
               {
                ScreenWidth=1024;
                ScreenHeight=768;
                FormRemoteControl->ShapeResolution->Width=102;
                FormRemoteControl->ShapeResolution->Height=77;
                FormRemoteControl->ShapeResolution->Brush->Color=clBlue;
                break;
               }
        case 2:
               {
                ScreenWidth=1280;
                ScreenHeight=720;
                FormRemoteControl->ShapeResolution->Width=128;
                FormRemoteControl->ShapeResolution->Height=72;
                FormRemoteControl->ShapeResolution->Brush->Color=clAqua;
                break;
               }
        case 3:
               {
                ScreenWidth=1280;
                ScreenHeight=768;
                FormRemoteControl->ShapeResolution->Width=128;
                FormRemoteControl->ShapeResolution->Height=77;
                FormRemoteControl->ShapeResolution->Brush->Color=clGreen;
                break;
               }
        case 4:
               {
                ScreenWidth=1280;
                ScreenHeight=800;
                FormRemoteControl->ShapeResolution->Width=128;
                FormRemoteControl->ShapeResolution->Height=80;
                FormRemoteControl->ShapeResolution->Brush->Color=clGreen;
                break;
               }
        case 5:
               {
                ScreenWidth=1280;
                ScreenHeight=1024;
                FormRemoteControl->ShapeResolution->Width=128;
                FormRemoteControl->ShapeResolution->Height=102;
                FormRemoteControl->ShapeResolution->Brush->Color=clBlue;
                break;
               }
        case 6:
               {
                ScreenWidth=1400;
                ScreenHeight=1050;
                FormRemoteControl->ShapeResolution->Width=140;
                FormRemoteControl->ShapeResolution->Height=105;
                FormRemoteControl->ShapeResolution->Brush->Color=clBlue;
                break;
               }
        case 7:
               {
                ScreenWidth=1600;
                ScreenHeight=1200;
                FormRemoteControl->ShapeResolution->Width=160;
                FormRemoteControl->ShapeResolution->Height=120;
                FormRemoteControl->ShapeResolution->Brush->Color=clBlue;
                break;
               }
        case 8:
               {
                ScreenWidth=1900;
                ScreenHeight=1600;
                FormRemoteControl->ShapeResolution->Width=190;
                FormRemoteControl->ShapeResolution->Height=160;
                FormRemoteControl->ShapeResolution->Brush->Color=clAqua;
                break;
               }
        case 9:
               {
                ScreenWidth=1920;
                ScreenHeight=1080;
                FormRemoteControl->ShapeResolution->Width=192;
                FormRemoteControl->ShapeResolution->Height=108;
                FormRemoteControl->ShapeResolution->Brush->Color=clAqua;
                break;
               }
        default: break;
       }
}
//---------------------------------------------------------------------------

void __fastcall TFormRemoteControl::ComPortRxChar(TObject *Sender, int Count)
{
 ComPort->ReadStr(Data,Count);
 FormRemoteControl->Character->Caption=Data;

 if (Data=='M')
   {
    CounterM++;

    if (CounterM==1 || CounterM==2)
      {
       MoveUp();
      }

    if (CounterM==3)
      {
       MoveUpFast();
      }

    if (CounterM==4)
      {
       CounterM=0;
      }
   }

 if (Data=='E')
   {
    CounterE++;

    if (CounterE==1 || CounterE==2)
      {
       MoveRightUp();
      }

    if (CounterE==3)
      {
       MoveRightUpFast();
      }

    if (CounterE==4)
      {
       CounterE=0;
      }
   }

 if (Data=='F')
   {
    CounterF++;

    if (CounterF==1 || CounterF==2)
      {
       MoveRight();
      }

    if (CounterF==3)
      {
       MoveRightFast();
      }

    if (CounterF==4)
      {
       CounterF=0;
      }
   }

 if (Data=='G')
   {
    CounterG++;

    if (CounterG==1 || CounterG==2)
      {
       MoveRightDown();
      }

    if (CounterG==3)
      {
       MoveRightDownFast();
      }

    if (CounterG==4)
      {
       CounterG=0;
      }
   }

 if (Data=='O')
   {
    CounterO++;

    if (CounterO==1 || CounterO==2)
      {
       MoveDown();
      }

    if (CounterO==3)
      {
       MoveDownFast();
      }

    if (CounterO==4)
      {
       CounterO=0;
      }
   }

 if (Data=='C')
   {
    CounterC++;

    if (CounterC==1 || CounterC==2)
      {
       MoveLeftDown();
      }

    if (CounterC==3)
      {
       MoveLeftDownFast();
      }

    if (CounterC==4)
      {
       CounterC=0;
      }
   }

 if (Data=='B')
   {
    CounterB++;

    if (CounterB==1 || CounterB==2)
      {
       MoveLeft();
      }

    if (CounterB==3)
      {
       MoveLeftFast();
      }

    if (CounterB==4)
      {
       CounterB=0;
      }
   }

 if (Data=='A')
   {
    CounterA++;

    if (CounterA==1 || CounterA==2)
      {
       MoveLeftUp();
      }

    if (CounterA==3)
      {
       MoveLeftUpFast();
      }

    if (CounterA==4)
      {
       CounterA=0;
      }
   }

 if (Data=='N')
   {
    CounterN++;
    if (CounterN==1)
      {
       mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP ,a,b,0,0);
      }
    if (CounterN==2)
      {
       GetCursorPos(&Coordinates);
       a=Coordinates.x;
       b=Coordinates.y;
       mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP ,a,b,0,0);
       Sleep(50);
       mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP ,a,b,0,0);
       ResetCounters();
      }

   }

  if (Data=='@')
    {
     ResetCounters();
     GetCursorPos(&Coordinates);
     a=Coordinates.x;
     b=Coordinates.y;
     mouse_event(MOUSEEVENTF_RIGHTDOWN,a,b,0,0);
     mouse_event(MOUSEEVENTF_RIGHTUP,a,b,0,0);
    }

  if (Data=='H')
    {
     CounterH++;

     if (CounterH == 1)
       {
        GetCursorPos(&Coordinates);
        a=Coordinates.x;
        b=Coordinates.y;
        mouse_event(MOUSEEVENTF_LEFTDOWN,a,b,0,0);
       }

    if (CounterH == 2)
      {
       GetCursorPos(&Coordinates);
       a=Coordinates.x;
       b=Coordinates.y;
       mouse_event(MOUSEEVENTF_LEFTUP ,a,b,0,0);
       ResetCounters();
      }
    }
}

//---------------------------------------------------------------------------

void __fastcall TFormRemoteControl::ScrollBarAccelerometerChange(TObject *Sender)
{
 Accelerator=FormRemoteControl->ScrollBarAccelerometer->Position;
 FormRemoteControl->StaticTextPixels->Caption=Accelerator;
 WaitPlease=101-Accelerator;
}
//---------------------------------------------------------------------------


